import React, { useMemo, useState } from 'react';
import { MainMenu } from 'base/ctl/menu';
import { Time } from 'base/ctl/base';
import { DlgLog } from 'base/ctl/log';
import { obj_int, uDb, cline, PanelBt, uPrint, MaTable, Wait, NuiBt } from 'sui';
import { loc } from 'local/index';
import { MtTitle, DlgNum } from './ctl';
import { up_qr, print_value, get_tp } from './cmd';

const Print = ({ wsn, hreturn }) => {
    const [cur, setCur] = useState();
    const [cur_out, curOut] = useState();
    const [open_menu, openMenu] = useState(false);
    const [open_num, openNum] = useState(false);
    const [open_log, openLog] = useState(false);
    const [wait, setWait] = useState(false);
    const [main, setMain] = useState();

    const getCol = useMemo(() => {
        const col = [
            {title: "物料批次号", field: 'sn', width: 100},
            {title: "球径", field: 'ball', width: 100},
        ];

        if (cur_out === "G" || cur_out === "T") {
            col.push({title: "规值", field: 'std', width: 120});
            col.push({title: "分规值", field: 'sub', width: 120});
        }

        if (cur_out === "S" || cur_out === "C") {
            col.push({title: "型号", field: 'tech', width: 100});
        }

        if (cur_out === "C") {
            col.push({title: "材料牌号", field: 'stuffnum', width: 100});
        }

        if (cur_out === "S") {
            col.push({title: "AB面", field: 'face', width: 100});
        }

        col.push({title: "二维码", field: 'qr', width: 100});
        col.push({title: loc("client_23"), field: 'e_opt', width: 100, render: d => {
            return <NuiBt className="ml-8 h-16" color="green" hcmd={() => {
                setCur(d);
                openNum(true);
            }}>打印</NuiBt>;
        }});

        return col;
    }, [cur_out]);

    const hPrint = (v) => {
        const box = obj_int(v, "box");
        const snum = obj_int(v, "snum");

        const tmp = {...cur};
        delete tmp["tableData"];

        up_qr(tmp, snum);

        uPrint(null, tmp);

        for (let i = 1; i < box; i++) {
            uPrint(null, tmp, null, null, false);
        }
    };

    const hSelect = (val) => {
        curOut(val);

        const mod = get_tp(val);
        if (!mod) {
            setMain(null);
            return;
        }

        setWait(true)

        uDb(null, `${mod}/list`,
            {
                line: cline(),
            },
            r => {
                const ret = r.map(d => {
                    const cmd = {
                        ...d,
                        tmpl: val.toLowerCase(),
                    };

                    if (val === "G" || val === "T") {
                        cmd["std"] = print_value(d.std);
                        cmd["sub"] = print_value(d.sub);
                    }

                    return cmd;
                });

                setMain(ret);
                setWait(false);
            },
            e => {
                setMain(null)
                setWait(false);
            }
        );
    };

    const menu = [{
        label: loc("base_157"),
        name: "log",
        hcmd: () => openLog(true),
    }];

    return (
        <div className="flex flex-col ml-2 mr-1">
            <div className="inline-flex mt-2 justify-between">
                <div className="inline-flex items-center">
                    <div className="ml-2 mr-4">
                        <MainMenu value={menu} open={open_menu} setOpen={openMenu}/>
                    </div>
                    <MtTitle sub="打印物料码" cur={cur}/>
                </div>
                <Time className="text-2xl"/>
                <NuiBt className="w-32 h-16" hcmd={hreturn}>返回</NuiBt>
            </div>
            <div className="inline-flex ml-2 mt-4">
                <PanelBt name="G" select={cur_out === "G"} hcmd={() => hSelect("G")} className="mr-4" cl="indigo">钢球</PanelBt>
                <PanelBt name="T" select={cur_out === "T"} hcmd={() => hSelect("T")} className="mr-4" cl="indigo">陶瓷球</PanelBt>
                <PanelBt name="C" select={cur_out === "C"} hcmd={() => hSelect("C")} className="mr-4" cl="indigo">保持架</PanelBt>
                <PanelBt name="S" select={cur_out === "S"} hcmd={() => hSelect("S")} cl="indigo">密封圈</PanelBt>
            </div>
            <div className="flex flex-row items-start mt-1">
                <div className="flex flex-col w-full">
                    <MaTable col={getCol} row={main} page={8} paging={true} search={true}
                    opt={{
                        minBodyHeight: 600,
                        maxBodyHeight: 600,
                    }}/>
                </div>
            </div>
            <DlgLog open={open_log} setOpen={openLog}/>
            <DlgNum open={open_num} setOpen={openNum} hcmd={hPrint}/>
            <Wait wait={wait}/>
        </div>
    );
};

export {Print};
